#!/usr/bin/php
<?php

/**
 * emailme -- by Dario Berzano <dario.berzano@gmail.com>
 *
 * Part of the lfscripts suite -- http://lfscripts.googecode.com/
 *
 * This PHP script allows you to send emails with a simple message in the header
 * and in the body, describing the operation (first parameter), and the status
 * of that operation (second parameter), which can be zero (success) or nonzero
 * (failure).
 *
 * More information can be provided as attached files (from third parameter on).
 *
 * This software contains, obfuscated for size reasons, the PHPMailer library,
 * which can be found at http://phpmailer.worxware.com/ and it's released under
 * LGPL.
 */


// PHP Mailer library - obfuscated, only SMTP class included
class PHPMailer {var $Priority= 3; var $CharSet = 'utf-8';var $ContentType= 'text/plain';var $Encoding= '8bit';var $ErrorInfo = '';var $From= 'root@localhost';var $FromName= 'Root User';var $Sender= '';var $Subject = '';var $Body= '';var $AltBody = '';var $WordWrap= 0;var $Mailer= 'mail';var $Sendmail= '/usr/sbin/sendmail';var $PluginDir = '';var $Version = "\062\x2e\060\056\063";var $ConfirmReadingTo= '';var $Hostname= '';var $MessageID = '';var $Host= 'localhost';var $Port= 25;var $Helo= '';var $SMTPSecure = "";var $SMTPAuth = false;var $Username = '';var $Password = '';var $Timeout= 10;var $SMTPDebug= false;var $SMTPKeepAlive = false;var $SingleTo = false; var $smtp= NULL;var $to= array();var $cc= array();var $bcc = array();var $ReplyTo = array();var $attachment= array();var $CustomHeader= array();var $message_type= '';var $boundary= array();var $language= array();var $error_count = 0;var $LE= "\r\n";var $sign_cert_file= "";var $sign_key_file = "";var $sign_key_pass = "";function IsHTML($bool) {if($bool == true) {$this->ContentType = 'text/html';} else {$this->ContentType = 'text/plain';}}function IsSMTP() {$this->Mailer = 'smtp';}function IsMail() {$this->Mailer = 'mail';}function IsSendmail() {$this->Mailer = 'sendmail';}function IsQmail() {$this->Sendmail = '/var/qmail/bin/sendmail';$this->Mailer = 'sendmail';}function AddAddress($address, $name = '') {$cur = count($this->to);$this->to[$cur][0] = trim($address);$this->to[$cur][1] = $name;}function AddCC($address, $name = '') {$cur = count($this->cc);$this->cc[$cur][0] = trim($address);$this->cc[$cur][1] = $name;}function AddBCC($address, $name = '') {$cur = count($this->bcc);$this->bcc[$cur][0] = trim($address);$this->bcc[$cur][1] = $name;}function AddReplyTo($address, $name = '') {$cur = count($this->ReplyTo);$this->ReplyTo[$cur][0] = trim($address);$this->ReplyTo[$cur][1] = $name;}function Send() {$header = '';$body = '';$result = true;if((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {$this->SetError($this->Lang('provide_address'));return false;}if(!empty($this->AltBody)) {$this->ContentType = 'multipart/alternative';}$this->error_count = 0; $this->SetMessageType();$header .= $this->CreateHeader();$body = $this->CreateBody();if($body == '') {return false;}switch($this->Mailer) {case 'sendmail':$result = $this->SendmailSend($header, $body);break;case 'smtp':$result = $this->SmtpSend($header, $body);break;case 'mail':$result = $this->MailSend($header, $body);break;default:$result = $this->MailSend($header, $body);break; }return $result;}function SendmailSend($header, $body) {if ($this->Sender != '') {$sendmail = sprintf("\045\x73\040\055\x6f\x69\040\055\146 \045\x73 \055\x74", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));} else {$sendmail = sprintf("\045\x73\x20\x2d\157\x69 \055\x74", escapeshellcmd($this->Sendmail));}if(!@$mail = popen($sendmail, 'w')) {$this->SetError($this->Lang('execute') . $this->Sendmail);return false;}fputs($mail, $header);fputs($mail, $body);$result = pclose($mail);if (version_compare(phpversion(), '4.2.3') == -1) {$result = $result >> 8 & 0xFF;}if($result != 0) {$this->SetError($this->Lang('execute') . $this->Sendmail);return false;}return true;}function MailSend($header, $body) {$to = '';for($i = 0; $i < count($this->to); $i++) {if($i != 0) { $to .= ', '; }$to .= $this->AddrFormat($this->to[$i]);}$toArr = split(',', $to);$params = sprintf("\x2doi -\146\040\x25\163", $this->Sender);if ($this->Sender != '' && strlen(ini_get('safe_mode')) < 1) {$old_from = ini_get('sendmail_from');ini_set('sendmail_from', $this->Sender);if ($this->SingleTo === true && count($toArr) > 1) {foreach ($toArr as $key => $val) {$rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);}} else {$rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);}} else {if ($this->SingleTo === true && count($toArr) > 1) {foreach ($toArr as $key => $val) {$rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);}} else {$rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header);}}if (isset($old_from)) {ini_set('sendmail_from', $old_from);}if(!$rt) {$this->SetError($this->Lang('instantiate'));return false;}return true;}function SmtpSend($header, $body) { $error = '';$bad_rcpt = array();if(!$this->SmtpConnect()) {return false;}$smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;if(!$this->smtp->Mail($smtp_from)) {$error = $this->Lang('from_failed') . $smtp_from;$this->SetError($error);$this->smtp->Reset();return false;}for($i = 0; $i < count($this->to); $i++) {if(!$this->smtp->Recipient($this->to[$i][0])) {$bad_rcpt[] = $this->to[$i][0];}}for($i = 0; $i < count($this->cc); $i++) {if(!$this->smtp->Recipient($this->cc[$i][0])) {$bad_rcpt[] = $this->cc[$i][0];}}for($i = 0; $i < count($this->bcc); $i++) {if(!$this->smtp->Recipient($this->bcc[$i][0])) {$bad_rcpt[] = $this->bcc[$i][0];}}if(count($bad_rcpt) > 0) { for($i = 0; $i < count($bad_rcpt); $i++) {if($i != 0) {$error .= ', ';}$error .= $bad_rcpt[$i];}$error = $this->Lang('recipients_failed') . $error;$this->SetError($error);$this->smtp->Reset();return false;}if(!$this->smtp->Data($header . $body)) {$this->SetError($this->Lang('data_not_accepted'));$this->smtp->Reset();return false;}if($this->SMTPKeepAlive == true) {$this->smtp->Reset();} else {$this->SmtpClose();}return true;}function SmtpConnect() {if($this->smtp == NULL) {$this->smtp = new SMTP();}$this->smtp->do_debug = $this->SMTPDebug;$hosts = explode(';', $this->Host);$index = 0;$connection = ($this->smtp->Connected());while($index < count($hosts) && $connection == false) {$hostinfo = array();if(eregi('^(.+):([0-9]+)$', $hosts[$index], $hostinfo)) {$host = $hostinfo[1];$port = $hostinfo[2];} else {$host = $hosts[$index];$port = $this->Port;}if($this->smtp->Connect(((!empty($this->SMTPSecure))?$this->SMTPSecure.'://':'').$host, $port, $this->Timeout)) {if ($this->Helo != '') {$this->smtp->Hello($this->Helo);} else {$this->smtp->Hello($this->ServerHostname());}$connection = true;if($this->SMTPAuth) {if(!$this->smtp->Authenticate($this->Username, $this->Password)) {$this->SetError($this->Lang('authenticate'));$this->smtp->Reset();$connection = false;}}}$index++;}if(!$connection) {$this->SetError($this->Lang('connect_host'));}return $connection;}function SmtpClose() {if($this->smtp != NULL) {if($this->smtp->Connected()) {$this->smtp->Quit();$this->smtp->Close();}}}function SetLanguage($lang_type, $lang_path = 'language/') {if(file_exists($lang_path.'phpmailer.lang-'.$lang_type.'.php')) {include($lang_path.'phpmailer.lang-'.$lang_type.'.php');} elseif (file_exists($lang_path.'phpmailer.lang-en.php')) {include($lang_path.'phpmailer.lang-en.php');} else {$PHPMAILER_LANG = array();$PHPMAILER_LANG["\160r\x6fvi\x64e\x5f\141\144\x64\162e\x73s"]= 'You must provide at least one ' .$PHPMAILER_LANG["\x6d\x61\x69\154\x65r_\x6eo\x74\137s\165\x70\x70\x6f\x72te\x64"] = ' mailer is not supported.';$PHPMAILER_LANG["\145x\x65\x63u\164\x65"]= 'Could not execute: ';$PHPMAILER_LANG["\151\156\163\x74\x61\156\x74\151\x61t\x65"]= 'Could not instantiate mail function.';$PHPMAILER_LANG["\x61\165t\150\x65\x6e\164i\x63\141\x74\x65"] = 'SMTP Error: Could not authenticate.';$PHPMAILER_LANG["\146\x72\x6fm\x5ff\141i\x6c\145d"]= 'The following From address failed: ';$PHPMAILER_LANG["\162eci\x70\x69\145\156t\x73_f\141\x69led"]= 'SMTP Error: The following ' .$PHPMAILER_LANG["d\x61t\141\137\156\x6ft\137a\x63\x63e\x70\x74ed"]= 'SMTP Error: Data not accepted.';$PHPMAILER_LANG["\x63\157\156\156e\x63\164_\x68\x6f\163t"] = 'SMTP Error: Could not connect to SMTP host.';$PHPMAILER_LANG["f\x69le\x5f\141\x63c\x65\163\x73"]= 'Could not access file: ';$PHPMAILER_LANG["\146i\x6c\x65\x5fo\160\145n"]= 'File Error: Could not open file: ';$PHPMAILER_LANG["\145\x6e\143\157di\x6e\147"] = 'Unknown encoding: ';$PHPMAILER_LANG["\x73\x69g\x6e\x69n\x67"]= 'Signing Error: ';}$this->language = $PHPMAILER_LANG;return true;}function AddrAppend($type, $addr) {$addr_str = $type . ': ';$addr_str .= $this->AddrFormat($addr[0]);if(count($addr) > 1) {for($i = 1; $i < count($addr); $i++) {$addr_str .= ', ' . $this->AddrFormat($addr[$i]);}}$addr_str .= $this->LE;return $addr_str;}function AddrFormat($addr) {if(empty($addr[1])) {$formatted = $this->SecureHeader($addr[0]);} else {$formatted = $this->EncodeHeader($this->SecureHeader($addr[1]), 'phrase') . "\040\x3c" . $this->SecureHeader($addr[0]) . "\x3e";}return $formatted;}function WrapText($message, $length, $qp_mode = false) {$soft_break = ($qp_mode) ? sprintf("\x20\075\x25\x73", $this->LE) : $this->LE; $is_utf8 = (strtolower($this->CharSet) == "ut\x66\0558");$message = $this->FixEOL($message);if (substr($message, -1) == $this->LE) {$message = substr($message, 0, -1);}$line = explode($this->LE, $message);$message = '';for ($i=0 ;$i < count($line); $i++) {$line_part = explode(' ', $line[$i]);$buf = '';for ($e = 0; $e<count($line_part); $e++) {$word = $line_part[$e];if ($qp_mode and (strlen($word) > $length)) {$space_left = $length - strlen($buf) - 1;if ($e != 0) {if ($space_left > 20) {$len = $space_left;if ($is_utf8) {$len = $this->UTF8CharBoundary($word, $len);} elseif (substr($word, $len - 1, 1) == "\075") {$len--;} elseif (substr($word, $len - 2, 1) == "\x3d") {$len -= 2;}$part = substr($word, 0, $len);$word = substr($word, $len);$buf .= ' ' . $part;$message .= $buf . sprintf("=\045s", $this->LE);} else {$message .= $buf . $soft_break;}$buf = '';}while (strlen($word) > 0) {$len = $length;if ($is_utf8) {$len = $this->UTF8CharBoundary($word, $len);} elseif (substr($word, $len - 1, 1) == "=") {$len--;} elseif (substr($word, $len - 2, 1) == "\075") {$len -= 2;}$part = substr($word, 0, $len);$word = substr($word, $len);if (strlen($word) > 0) {$message .= $part . sprintf("\x3d\045s", $this->LE);} else {$buf = $part;}}} else {$buf_o = $buf;$buf .= ($e == 0) ? $word : (' ' . $word);if (strlen($buf) > $length and $buf_o != '') {$message .= $buf_o . $soft_break;$buf = $word;}}}$message .= $buf . $this->LE;}return $message;}function UTF8CharBoundary($encodedText, $maxLength) {$foundSplitPos = false;$lookBack = 3;while (!$foundSplitPos) {$lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);$encodedCharPos = strpos($lastChunk, "=");if ($encodedCharPos !== false) { $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);$dec = hexdec($hex);if ($dec < 128) { $maxLength = ($encodedCharPos == 0) ? $maxLength :$maxLength - ($lookBack - $encodedCharPos);$foundSplitPos = true;} elseif ($dec >= 192) { $maxLength = $maxLength - ($lookBack - $encodedCharPos);$foundSplitPos = true;} elseif ($dec < 192) { $lookBack += 3;}} else { $foundSplitPos = true;}}return $maxLength;}function SetWordWrap() {if($this->WordWrap < 1) {return;}switch($this->message_type) {case 'alt':case 'alt_attachments':$this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);break;default:$this->Body = $this->WrapText($this->Body, $this->WordWrap);break;}}function CreateHeader() {$result = '';$uniq_id = md5(uniqid(time()));$this->boundary[1] = 'b1_' . $uniq_id;$this->boundary[2] = 'b2_' . $uniq_id;$result .= $this->HeaderLine('Date', $this->RFCDate());if($this->Sender == '') {$result .= $this->HeaderLine('Return-Path', trim($this->From));} else {$result .= $this->HeaderLine('Return-Path', trim($this->Sender));}if($this->Mailer != 'mail') {if(count($this->to) > 0) {$result .= $this->AddrAppend('To', $this->to);} elseif (count($this->cc) == 0) {$result .= $this->HeaderLine('To', 'undisclosed-recipients:;');}}$from = array();$from[0][0] = trim($this->From);$from[0][1] = $this->FromName;$result .= $this->AddrAppend('From', $from);if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->cc) > 0)) {$result .= $this->AddrAppend('Cc', $this->cc);}if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) {$result .= $this->AddrAppend('Bcc', $this->bcc);}if(count($this->ReplyTo) > 0) {$result .= $this->AddrAppend('Reply-To', $this->ReplyTo);}if($this->Mailer != 'mail') {$result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject)));}if($this->MessageID != '') {$result .= $this->HeaderLine('Message-ID',$this->MessageID);} else {$result .= sprintf("\115e\163\x73\x61ge\x2dID\x3a \074%\163\100\045s\076%\163", $uniq_id, $this->ServerHostname(), $this->LE);}$result .= $this->HeaderLine('X-Priority', $this->Priority);$result .= $this->HeaderLine('X-Mailer', 'PHPMailer (phpmailer.sourceforge.net) [version ' . $this->Version . ']');if($this->ConfirmReadingTo != '') {$result .= $this->HeaderLine('Disposition-Notification-To', '<' . trim($this->ConfirmReadingTo) . '>');} for($index = 0; $index < count($this->CustomHeader); $index++) {$result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), $this->EncodeHeader(trim($this->CustomHeader[$index][1])));}if (!$this->sign_key_file) {$result .= $this->HeaderLine('MIME-Version', '1.0');$result .= $this->GetMailMIME();}return $result;}function GetMailMIME() {$result = '';switch($this->message_type) {case 'plain':$result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);$result .= sprintf("\x43o\156\x74\145\156\x74\055Ty\x70\x65:\x20\045\x73;\040ch\x61\162\x73\x65t\075\"\x25\x73\"", $this->ContentType, $this->CharSet);break;case 'attachments':case 'alt_attachments':if($this->InlineImageExists()){$result .= sprintf("C\x6fn\164\145\x6e\164\055\x54\171\x70\x65\072\040%s\x3b\x25\163\t\x74\171\x70\145=\"te\x78\x74\x2fh\164\155\x6c\";\045\x73\t\142ou\x6e\x64\x61\x72\x79\075\"%s\"\045s", 'multipart/related', $this->LE, $this->LE, $this->boundary[1], $this->LE);} else {$result .= $this->HeaderLine('Content-Type', 'multipart/mixed;');$result .= $this->TextLine("\t\x62\157un\x64\x61\162y=\"" . $this->boundary[1] . '"');}break;case 'alt':$result .= $this->HeaderLine('Content-Type', 'multipart/alternative;');$result .= $this->TextLine("\tb\x6f\x75nd\x61r\x79\075\"" . $this->boundary[1] . '"');break;}if($this->Mailer != 'mail') {$result .= $this->LE.$this->LE;}return $result;}function CreateBody() {$result = '';if ($this->sign_key_file) {$result .= $this->GetMailMIME();}$this->SetWordWrap();switch($this->message_type) {case 'alt':$result .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');$result .= $this->EncodeString($this->AltBody, $this->Encoding);$result .= $this->LE.$this->LE;$result .= $this->GetBoundary($this->boundary[1], '', 'text/html', '');$result .= $this->EncodeString($this->Body, $this->Encoding);$result .= $this->LE.$this->LE;$result .= $this->EndBoundary($this->boundary[1]);break;case 'plain':$result .= $this->EncodeString($this->Body, $this->Encoding);break;case 'attachments':$result .= $this->GetBoundary($this->boundary[1], '', '', '');$result .= $this->EncodeString($this->Body, $this->Encoding);$result .= $this->LE;$result .= $this->AttachAll();break;case 'alt_attachments':$result .= sprintf("\055-\x25\x73%\163", $this->boundary[1], $this->LE);$result .= sprintf("\x43\157\x6e\x74\145nt\055\124\x79\x70\x65:\040\045\163\073\x25s" . "\t\142\x6f\165n\144\x61\x72\x79\x3d\"\045\x73\"%\163", 'multipart/alternative', $this->LE, $this->boundary[2], $this->LE.$this->LE);$result .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '') . $this->LE; $result .= $this->EncodeString($this->AltBody, $this->Encoding);$result .= $this->LE.$this->LE;$result .= $this->GetBoundary($this->boundary[2], '', 'text/html', '') . $this->LE; $result .= $this->EncodeString($this->Body, $this->Encoding);$result .= $this->LE.$this->LE;$result .= $this->EndBoundary($this->boundary[2]);$result .= $this->AttachAll();break;}if($this->IsError()) {$result = '';} else if ($this->sign_key_file) {$file = tempnam("", "\x6da\x69l");$fp = fopen($file, "\167");fwrite($fp, $result);fclose($fp);$signed = tempnam("", "sig\x6e\145d");if (@openssl_pkcs7_sign($file, $signed, "f\151\154\145\072\057/".$this->sign_cert_file, array("\x66\x69\x6c\145\072//".$this->sign_key_file, $this->sign_key_pass), null)) {$fp = fopen($signed, "\x72");$result = fread($fp, filesize($this->sign_key_file));$result = '';while(!feof($fp)){$result = $result . fread($fp, 1024);}fclose($fp);} else {$this->SetError($this->Lang("si\x67ni\x6e\147").openssl_error_string());$result = '';}unlink($file);unlink($signed);}return $result;}function GetBoundary($boundary, $charSet, $contentType, $encoding) {$result = '';if($charSet == '') {$charSet = $this->CharSet;}if($contentType == '') {$contentType = $this->ContentType;}if($encoding == '') {$encoding = $this->Encoding;}$result .= $this->TextLine('--' . $boundary);$result .= sprintf("\103o\x6et\145\156\x74\055\124\171\160\145\072\x20\x25\x73\073\x20\143\x68\x61\162set\x20\x3d\x20\"\045\163\"", $contentType, $charSet);$result .= $this->LE;$result .= $this->HeaderLine('Content-Transfer-Encoding', $encoding);$result .= $this->LE;return $result;}function EndBoundary($boundary) {return $this->LE . '--' . $boundary . '--' . $this->LE;}function SetMessageType() {if(count($this->attachment) < 1 && strlen($this->AltBody) < 1) {$this->message_type = 'plain';} else {if(count($this->attachment) > 0) {$this->message_type = 'attachments';}if(strlen($this->AltBody) > 0 && count($this->attachment) < 1) {$this->message_type = 'alt';}if(strlen($this->AltBody) > 0 && count($this->attachment) > 0) {$this->message_type = 'alt_attachments';}}}function HeaderLine($name, $value) {return $name . ': ' . $value . $this->LE;}function TextLine($value) {return $value . $this->LE;}function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {if(!@is_file($path)) {$this->SetError($this->Lang('file_access') . $path);return false;}$filename = basename($path);if($name == '') {$name = $filename;}$cur = count($this->attachment);$this->attachment[$cur][0] = $path;$this->attachment[$cur][1] = $filename;$this->attachment[$cur][2] = $name;$this->attachment[$cur][3] = $encoding;$this->attachment[$cur][4] = $type;$this->attachment[$cur][5] = false; $this->attachment[$cur][6] = 'attachment';$this->attachment[$cur][7] = 0;return true;}function AttachAll() {$mime = array();for($i = 0; $i < count($this->attachment); $i++) {$bString = $this->attachment[$i][5];if ($bString) {$string = $this->attachment[$i][0];} else {$path = $this->attachment[$i][0];}$filename= $this->attachment[$i][1];$name= $this->attachment[$i][2];$encoding= $this->attachment[$i][3];$type= $this->attachment[$i][4];$disposition = $this->attachment[$i][6];$cid = $this->attachment[$i][7];$mime[] = sprintf("\x2d\055%\x73%\x73", $this->boundary[1], $this->LE);$mime[] = sprintf("C\157\156\x74\x65\x6e\x74-T\x79\x70\x65\072\x20\045\x73\073\040\x6e\x61me\075\"\x25s\"\045\163", $type, $this->EncodeHeader($this->SecureHeader($name)), $this->LE);$mime[] = sprintf("\x43\x6fn\x74e\156\x74\x2d\x54\162a\x6e\163f\x65\162-\105\x6e\143\157ding:\040%\x73%\x73", $encoding, $this->LE);if($disposition == 'inline') {$mime[] = sprintf("Con\164\x65n\164-\x49\x44\072\x20\074\045s>%\163", $cid, $this->LE);}$mime[] = sprintf("Conten\164-D\x69s\x70\157\x73\151\x74\151\157n:\x20%\163;\040\146\151\x6ce\156\141\x6de\x3d\"\x25s\"\x25\x73", $disposition, $this->EncodeHeader($this->SecureHeader($name)), $this->LE.$this->LE);if($bString) {$mime[] = $this->EncodeString($string, $encoding);if($this->IsError()) {return '';}$mime[] = $this->LE.$this->LE;} else {$mime[] = $this->EncodeFile($path, $encoding);if($this->IsError()) {return '';}$mime[] = $this->LE.$this->LE;}}$mime[] = sprintf("-\055%s\055\x2d%s", $this->boundary[1], $this->LE);return join('', $mime);}function EncodeFile ($path, $encoding = 'base64') {if(!@$fd = fopen($path, 'rb')) {$this->SetError($this->Lang('file_open') . $path);return '';}$magic_quotes = get_magic_quotes_runtime();set_magic_quotes_runtime(0);$file_buffer = fread($fd, filesize($path));$file_buffer = $this->EncodeString($file_buffer, $encoding);fclose($fd);set_magic_quotes_runtime($magic_quotes);return $file_buffer;}function EncodeString ($str, $encoding = 'base64') {$encoded = '';switch(strtolower($encoding)) {case 'base64':$encoded = chunk_split(base64_encode($str), 76, $this->LE);break;case '7bit':case '8bit':$encoded = $this->FixEOL($str);if (substr($encoded, -(strlen($this->LE))) != $this->LE)$encoded .= $this->LE;break;case 'binary':$encoded = $str;break;case 'quoted-printable':$encoded = $this->EncodeQP($str);break;default:$this->SetError($this->Lang('encoding') . $encoding);break;}return $encoded;}function EncodeHeader ($str, $position = 'text') {$x = 0;switch (strtolower($position)) {case 'phrase':if (!preg_match('/[\200-\377]/', $str)) {$encoded = addcslashes($str, "\0\x2e\056\3\067\177\\\"");if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {return ($encoded);} else {return ("\"$encoded\"");}}$x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);break;case 'comment':$x = preg_match_all('/[()"]/', $str, $matches);case 'text':default:$x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);break;}if ($x == 0) {return ($str);}$maxlen = 75 - 7 - strlen($this->CharSet);if (strlen($str)/3 < $x) {$encoding = 'B';if (function_exists('mb_strlen') && $this->HasMultiBytes($str)) { $encoded = $this->Base64EncodeWrapMB($str);} else {$encoded = base64_encode($str);$maxlen -= $maxlen % 4;$encoded = trim(chunk_split($encoded, $maxlen, "\n"));}} else {$encoding = 'Q';$encoded = $this->EncodeQ($str, $position);$encoded = $this->WrapText($encoded, $maxlen, true);$encoded = str_replace('='.$this->LE, "\n", trim($encoded));}$encoded = preg_replace('/^(.*)$/m', "\040=\077".$this->CharSet."?$encoding?\\\061\x3f\x3d", $encoded);$encoded = trim(str_replace("\n", $this->LE, $encoded));return $encoded;}function HasMultiBytes($str) {if (function_exists('mb_strlen')) {return (strlen($str) > mb_strlen($str, $this->CharSet));} else { return False;}}function Base64EncodeWrapMB($str) {$start = "\x3d?".$this->CharSet."?\102?";$end = "?\075";$encoded = "";$mb_length = mb_strlen($str, $this->CharSet); $length = 75 - strlen($start) - strlen($end); $ratio = $mb_length / strlen($str); $offset = $avgLength = floor($length * $ratio * .75);for ($i = 0; $i < $mb_length; $i += $offset) {$lookBack = 0;do {$offset = $avgLength - $lookBack;$chunk = mb_substr($str, $i, $offset, $this->CharSet);$chunk = base64_encode($chunk);$lookBack++;}while (strlen($chunk) > $length);$encoded .= $chunk . $this->LE;} $encoded = substr($encoded, 0, -strlen($this->LE));return $encoded;}function EncodeQP( $input = '', $line_max = 76, $space_conv = false ) {$hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');$lines = preg_split('/(?:\r\n|\r|\n)/', $input);$eol = "\r\n";$escape = '=';$output = '';while( list(, $line) = each($lines) ) {$linlen = strlen($line);$newline = '';for($i = 0; $i < $linlen; $i++) {$c = substr( $line, $i, 1 );$dec = ord( $c );if ( ( $i == 0 ) && ( $dec == 46 ) ) { $c = '=2E';}if ( $dec == 32 ) {if ( $i == ( $linlen - 1 ) ) { $c = '=20';} else if ( $space_conv ) {$c = '=20';}} elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { $h2 = floor($dec/16);$h1 = floor($dec%16);$c = $escape.$hex[$h2].$hex[$h1];}if ( (strlen($newline) + strlen($c)) >= $line_max ) { $output .= $newline.$escape.$eol; $newline = ''; if ( $dec == 46 ) {$c = '=2E';}}$newline .= $c;} $output .= $newline.$eol;} return $output;}function EncodeQ ($str, $position = 'text') {$encoded = preg_replace("[\r\n\135", '', $str);switch (strtolower($position)) {case 'phrase':$encoded = preg_replace("\x2f([\136A-\x5aa\x2d\x7a0\x2d9\x21\052\053\/\040\x2d\135\051\057\x65", "'\075'\056\163\x70r\151ntf\050'\045\0602X', o\162\x64\x28'\\\061'\x29\x29", $encoded);break;case 'comment':$encoded = preg_replace("\x2f\050\x5b\(\)\"\x5d)\057\x65", "'\075'\x2e\x73p\162\x69\156t\x66('%\x30\x32X'\x2c\040ord\x28'\\\x31'\x29)", $encoded);case 'text':default:$encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',"'='\056\x73\x70\x72\151n\x74f\050'%\x30\x32\130', \157\162\144\x28'\\\061')\x29", $encoded);break;}$encoded = str_replace(' ', '_', $encoded);return $encoded;}function AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') {$cur = count($this->attachment);$this->attachment[$cur][0] = $string;$this->attachment[$cur][1] = $filename;$this->attachment[$cur][2] = $filename;$this->attachment[$cur][3] = $encoding;$this->attachment[$cur][4] = $type;$this->attachment[$cur][5] = true; $this->attachment[$cur][6] = 'attachment';$this->attachment[$cur][7] = 0;}function AddEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {if(!@is_file($path)) {$this->SetError($this->Lang('file_access') . $path);return false;}$filename = basename($path);if($name == '') {$name = $filename;}$cur = count($this->attachment);$this->attachment[$cur][0] = $path;$this->attachment[$cur][1] = $filename;$this->attachment[$cur][2] = $name;$this->attachment[$cur][3] = $encoding;$this->attachment[$cur][4] = $type;$this->attachment[$cur][5] = false;$this->attachment[$cur][6] = 'inline';$this->attachment[$cur][7] = $cid;return true;}function InlineImageExists() {$result = false;for($i = 0; $i < count($this->attachment); $i++) {if($this->attachment[$i][6] == 'inline') {$result = true;break;}}return $result;}function ClearAddresses() {$this->to = array();}function ClearCCs() {$this->cc = array();}function ClearBCCs() {$this->bcc = array();}function ClearReplyTos() {$this->ReplyTo = array();}function ClearAllRecipients() {$this->to = array();$this->cc = array();$this->bcc = array();}function ClearAttachments() {$this->attachment = array();}function ClearCustomHeaders() {$this->CustomHeader = array();}function SetError($msg) {$this->error_count++;$this->ErrorInfo = $msg;}function RFCDate() {$tz = date('Z');$tzs = ($tz < 0) ? '-' : '+';$tz = abs($tz);$tz = (int)($tz/3600)*100 + ($tz%3600)/60;$result = sprintf("\045\x73\x20%\x73\0450\064d", date('D, j M Y H:i:s'), $tzs, $tz);return $result;}function ServerVar($varName) {global $HTTP_SERVER_VARS;global $HTTP_ENV_VARS;if(!isset($_SERVER)) {$_SERVER = $HTTP_SERVER_VARS;if(!isset($_SERVER['REMOTE_ADDR'])) {$_SERVER = $HTTP_ENV_VARS; }}if(isset($_SERVER[$varName])) {return $_SERVER[$varName];} else {return '';}}function ServerHostname() {if ($this->Hostname != '') {$result = $this->Hostname;} elseif ($this->ServerVar('SERVER_NAME') != '') {$result = $this->ServerVar('SERVER_NAME');} else {$result = 'localhost.localdomain';}return $result;}function Lang($key) {if(count($this->language) < 1) {$this->SetLanguage('en'); }if(isset($this->language[$key])) {return $this->language[$key];} else {return 'Language string failed to load: ' . $key;}}function IsError() {return ($this->error_count > 0);}function FixEOL($str) {$str = str_replace("\r\n", "\n", $str);$str = str_replace("\r", "\n", $str);$str = str_replace("\n", $this->LE, $str);return $str;}function AddCustomHeader($custom_header) {$this->CustomHeader[] = explode(':', $custom_header, 2);}function MsgHTML($message,$basedir='') {preg_match_all("\x2f(\x73\x72c|\142ac\153\x67r\157u\156\144\x29\x3d\"(\x2e\x2a\x29\"/\125i", $message, $images);if(isset($images[2])) {foreach($images[2] as $i => $url) { if (!preg_match('/^[A-z][A-z]*:\/\//',$url)) {$filename = basename($url);$directory = dirname($url);($directory == '.')?$directory='':'';$cid = 'cid:' . md5($filename);$fileParts = split("\.", $filename);$ext = $fileParts[1];$mimeType = $this->_mime_types($ext);if ( strlen($basedir) > 1 && substr($basedir,-1) != '/') { $basedir .= '/'; }if ( strlen($directory) > 1 && substr($directory,-1) != '/') { $directory .= '/'; }if ( $this->AddEmbeddedImage($basedir.$directory.$filename, md5($filename), $filename, 'base64',$mimeType) ) {$message = preg_replace("\057".$images[1][$i]."\075\"".preg_quote($url, '/')."\"\x2f\125i", $images[1][$i]."\075\"".$cid."\"", $message);}}}}$this->IsHTML(true);$this->Body = $message;$textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s','',$message)));if ( !empty($textMsg) && empty($this->AltBody) ) {$this->AltBody = html_entity_decode($textMsg);}if ( empty($this->AltBody) ) {$this->AltBody = 'To view this email message, open the email in with HTML compatibility!' . "\n\n";}}function _mime_types($ext = '') {$mimes = array('ai'=>'application/postscript','aif' =>'audio/x-aiff','aifc'=>'audio/x-aiff','aiff'=>'audio/x-aiff','avi' =>'video/x-msvideo','bin' =>'application/macbinary','bmp' =>'image/bmp','class' =>'application/octet-stream','cpt' =>'application/mac-compactpro','css' =>'text/css','dcr' =>'application/x-director','dir' =>'application/x-director','dll' =>'application/octet-stream','dms' =>'application/octet-stream','doc' =>'application/msword','dvi' =>'application/x-dvi','dxr' =>'application/x-director','eml' =>'message/rfc822','eps' =>'application/postscript','exe' =>'application/octet-stream','gif' =>'image/gif','gtar'=>'application/x-gtar','htm' =>'text/html','html'=>'text/html','jpe' =>'image/jpeg','jpeg'=>'image/jpeg','jpg' =>'image/jpeg','hqx' =>'application/mac-binhex40','js'=>'application/x-javascript','lha' =>'application/octet-stream','log' =>'text/plain','lzh' =>'application/octet-stream','mid' =>'audio/midi','midi'=>'audio/midi','mif' =>'application/vnd.mif','mov' =>'video/quicktime','movie' =>'video/x-sgi-movie','mp2' =>'audio/mpeg','mp3' =>'audio/mpeg','mpe' =>'video/mpeg','mpeg'=>'video/mpeg','mpg' =>'video/mpeg','mpga'=>'audio/mpeg','oda' =>'application/oda','pdf' =>'application/pdf','php' =>'application/x-httpd-php','php3'=>'application/x-httpd-php','php4'=>'application/x-httpd-php','phps'=>'application/x-httpd-php-source','phtml' =>'application/x-httpd-php','png' =>'image/png','ppt' =>'application/vnd.ms-powerpoint','ps'=>'application/postscript','psd' =>'application/octet-stream','qt'=>'video/quicktime','ra'=>'audio/x-realaudio','ram' =>'audio/x-pn-realaudio','rm'=>'audio/x-pn-realaudio','rpm' =>'audio/x-pn-realaudio-plugin','rtf' =>'text/rtf','rtx' =>'text/richtext','rv'=>'video/vnd.rn-realvideo','sea' =>'application/octet-stream','shtml' =>'text/html','sit' =>'application/x-stuffit','so'=>'application/octet-stream','smi' =>'application/smil','smil'=>'application/smil','swf' =>'application/x-shockwave-flash','tar' =>'application/x-tar','text'=>'text/plain','txt' =>'text/plain','tgz' =>'application/x-tar','tif' =>'image/tiff','tiff'=>'image/tiff','wav' =>'audio/x-wav','wbxml' =>'application/vnd.wap.wbxml','wmlc'=>'application/vnd.wap.wmlc','word'=>'application/msword','xht' =>'application/xhtml+xml','xhtml' =>'application/xhtml+xml','xl'=>'application/excel','xls' =>'application/vnd.ms-excel','xml' =>'text/xml','xsl' =>'text/xml','zip' =>'application/zip');return ( ! isset($mimes[strtolower($ext)])) ? 'application/octet-stream' : $mimes[strtolower($ext)];}function set ( $name, $value = '' ) {if ( isset($this->$name) ) {$this->$name = $value;} else {$this->SetError('Cannot set or reset variable ' . $name);return false;}}function getFile($filename) {$return = '';if ($fp = fopen($filename, 'rb')) {while (!feof($fp)) {$return .= fread($fp, 1024);}fclose($fp);return $return;} else {return false;}}function SecureHeader($str) {$str = trim($str);$str = str_replace("\r", "", $str);$str = str_replace("\n", "", $str);return $str;}function Sign($cert_filename, $key_filename, $key_pass) {$this->sign_cert_file = $cert_filename;$this->sign_key_file = $key_filename;$this->sign_key_pass = $key_pass;}}class SMTP{var $SMTP_PORT = 25;var $CRLF = "\r\n";var $do_debug;var $do_verp = false;var $smtp_conn; var $error; var $helo_rply;function SMTP() {$this->smtp_conn = 0;$this->error = null;$this->helo_rply = null;$this->do_debug = 0;}function Connect($host,$port=0,$tval=30) { $this->error = null; if($this->connected()) { $this->error = array("e\x72ro\162" => "\101\154\x72\145\x61\144\171\040\x63o\156ne\143t\145d \x74o\040\x61\x20\x73\145r\x76\x65\x72");return false;}if(empty($port)) {$port = $this->SMTP_PORT;} $this->smtp_conn = fsockopen($host,$port,$errno,$errstr,$tval); if(empty($this->smtp_conn)) {$this->error = array("e\x72\x72\157\x72" => "\x46\x61\x69l\145\x64\040\164\157\x20\143\157nne\143\164 to\040ser\x76\145r", "\x65\x72r\x6eo" => $errno, "\x65\x72rst\162" => $errstr);if($this->do_debug >= 1) {echo "\x53\115\124P\x20-\076\040\105\122R\x4f\122\072 " . $this->error["err\x6fr"] . ": $errstr \050$errno)" . $this->CRLF;}return false;} if(substr(PHP_OS, 0, 3) != "\127\111\116") socket_set_timeout($this->smtp_conn, $tval, 0); $announce = $this->get_lines(); if($this->do_debug >= 2) {echo "SM\124\120\x20\055>\x20\106\122OM\x20\x53\x45\x52\126\105R\x3a" . $this->CRLF . $announce;}return true;}function Authenticate($username, $password) { fputs($this->smtp_conn,"\101\x55\x54\x48\x20L\x4f\x47\x49N" . $this->CRLF);$rply = $this->get_lines();$code = substr($rply,0,3);if($code != 334) {$this->error =array("e\x72ror" => "\x41U\x54\x48\040\156o\164\040\141c\143\x65\x70\x74e\x64 \x66ro\x6d\x20\163\145\162v\145r","s\x6d\164\x70\137\x63\157d\x65" => $code,"s\x6d\164\160\137ms\x67" => substr($rply,4));if($this->do_debug >= 1) {echo "\x53M\124\x50\040\x2d\x3e\040\x45\x52R\117\122\x3a\x20" . $this->error["\x65\162\x72or"] . "\x3a " . $rply . $this->CRLF;}return false;} fputs($this->smtp_conn, base64_encode($username) . $this->CRLF);$rply = $this->get_lines();$code = substr($rply,0,3);if($code != 334) {$this->error =array("err\157\x72" => "\x55\x73\x65\x72\156a\155e \156\157t\x20ac\x63e\x70t\145\x64\x20\x66\162\157\x6d\x20s\145rv\x65r","\x73mt\160\x5f\143o\144e" => $code,"\163mt\x70_ms\x67" => substr($rply,4));if($this->do_debug >= 1) {echo "\x53\x4dTP\x20\055\x3e \x45\x52\122\x4f\122\x3a " . $this->error["e\162r\157\x72"] . "\072\040" . $rply . $this->CRLF;}return false;} fputs($this->smtp_conn, base64_encode($password) . $this->CRLF);$rply = $this->get_lines();$code = substr($rply,0,3);if($code != 235) {$this->error =array("e\162r\x6f\162" => "\120\141ss\167\x6f\162\144 \x6e\157t \x61\143ce\160\164e\x64\040\146r\x6f\x6d\x20\163\x65\162\x76\x65\x72","s\155\x74p\x5fcod\145" => $code,"\x73\155\164\160\137m\x73\147" => substr($rply,4));if($this->do_debug >= 1) {echo "S\115TP\040\055> \105\x52\x52O\122\072\x20" . $this->error["e\162\x72\x6fr"] . ":\040" . $rply . $this->CRLF;}return false;}return true;}function Connected() {if(!empty($this->smtp_conn)) {$sock_status = socket_get_status($this->smtp_conn);if($sock_status["\x65\157\146"]) { if($this->do_debug >= 1) {echo "SM\124P \x2d\076\040\116\117\x54I\x43\x45:" . $this->CRLF . "\x45O\x46\x20\x63a\165g\x68t\x20\167hi\x6c\x65\x20\x63\x68\x65\x63\x6bi\156\x67\x20\151\146\x20co\156\156\145\143\164\145d";}$this->Close();return false;}return true; }return false;}function Close() {$this->error = null; $this->helo_rply = null;if(!empty($this->smtp_conn)) { fclose($this->smtp_conn);$this->smtp_conn = 0;}}function Data($msg_data) {$this->error = null; if(!$this->connected()) {$this->error = array("e\162\162\157\162" => "C\x61\154\154ed \x44\x61\x74a\x28)\040\x77i\x74h\157\x75\164 b\145i\156g \143\157\x6en\145c\164e\x64");return false;}fputs($this->smtp_conn,"\x44\101\124\101" . $this->CRLF);$rply = $this->get_lines();$code = substr($rply,0,3);if($this->do_debug >= 2) {echo "\123\115\x54\120\x20\x2d\x3e\x20\106\122\x4fM\040\123\x45R\126\105\122:" . $this->CRLF . $rply;}if($code != 354) {$this->error =array("\x65r\x72\x6f\162" => "\x44\x41\124\x41 \x63om\x6d\x61\x6e\144\040\156\x6f\164\040acc\145\x70t\145d\040\x66r\x6fm\040\x73\145r\166\x65\x72","\163\x6d\164\160\137\143\157\144e" => $code,"\163m\164\160_\155s\147" => substr($rply,4));if($this->do_debug >= 1) {echo "\123M\x54\x50\x20\055> ER\x52\117\122\x3a\040" . $this->error["\x65\x72\x72\x6f\162"] . "\072\040" . $rply . $this->CRLF;}return false;} $msg_data = str_replace("\r\n","\n",$msg_data);$msg_data = str_replace("\r","\n",$msg_data);$lines = explode("\n",$msg_data); $field = substr($lines[0],0,strpos($lines[0],"\x3a"));$in_headers = false;if(!empty($field) && !strstr($field," ")) {$in_headers = true;}$max_line_length = 998; while(list(,$line) = @each($lines)) {$lines_out = null;if($line == "" && $in_headers) {$in_headers = false;} while(strlen($line) > $max_line_length) {$pos = strrpos(substr($line,0,$max_line_length),"\040"); if(!$pos) {$pos = $max_line_length - 1;}$lines_out[] = substr($line,0,$pos);$line = substr($line,$pos + 1); if($in_headers) {$line = "\t" . $line;}}$lines_out[] = $line; while(list(,$line_out) = @each($lines_out)) {if(strlen($line_out) > 0){if(substr($line_out, 0, 1) == "\056") {$line_out = "\x2e" . $line_out;}}fputs($this->smtp_conn,$line_out . $this->CRLF);}} fputs($this->smtp_conn, $this->CRLF . "." . $this->CRLF);$rply = $this->get_lines();$code = substr($rply,0,3);if($this->do_debug >= 2) {echo "\x53\x4dTP \055\076\040F\x52\117\115\040\x53\x45\x52\126\105\x52\x3a" . $this->CRLF . $rply;}if($code != 250) {$this->error =array("e\162ro\x72" => "\x44A\124\101\040n\157\164\040\141\143cep\x74\145d\040\146\162\157\155 se\162\x76er","\x73\155\164\x70_c\157de" => $code,"sm\x74\160_m\x73g" => substr($rply,4));if($this->do_debug >= 1) {echo "\x53M\x54P\x20-\076\040E\122R\117\122\x3a\x20" . $this->error["\145rr\157r"] . "\x3a " . $rply . $this->CRLF;}return false;}return true;}function Expand($name) {$this->error = null; if(!$this->connected()) {$this->error = array("\145rr\157\162" => "\103all\145d \105\170\160\x61\x6e\144\x28\051 w\151\164h\x6f\x75\x74 \x62\x65i\156g\x20\x63o\x6e\156\x65c\164ed");return false;}fputs($this->smtp_conn,"\105X\120\x4e " . $name . $this->CRLF);$rply = $this->get_lines();$code = substr($rply,0,3);if($this->do_debug >= 2) {echo "S\115\124P \x2d\076\x20\x46\122\117M SE\x52\x56\105\x52:" . $this->CRLF . $rply;}if($code != 250) {$this->error =array("\x65r\162o\x72" => "\105\x58\120\x4e\x20\x6e\157\x74\x20\x61cce\160ted \146\162\x6fm\040\163\145\x72\x76e\x72","\163\155t\x70\x5f\x63\157\144e" => $code,"sm\164\x70\137\x6d\x73\x67" => substr($rply,4));if($this->do_debug >= 1) {echo "SM\x54P\040\055\x3e \x45\122\x52O\122\x3a\x20" . $this->error["\x65r\x72\x6fr"] . "\x3a\x20" . $rply . $this->CRLF;}return false;} $entries = explode($this->CRLF,$rply);while(list(,$l) = @each($entries)) {$list[] = substr($l,4);}return $list;}function Hello($host="") {$this->error = null; if(!$this->connected()) {$this->error = array("\145rror" => "\103\x61\x6cl\145\144\040\110\145\154\x6co\050\051 \167i\164\150\x6f\x75t\x20b\x65\x69\156g\040\143\157n\x6eect\x65\144");return false;} if(empty($host)) { $host = "\x6co\143\x61\x6c\150\157s\164";} if(!$this->SendHello("\105\110\114\x4f", $host)){if(!$this->SendHello("\x48\105\x4c\117", $host))return false;}return true;}function SendHello($hello, $host) {fputs($this->smtp_conn, $hello . "\x20" . $host . $this->CRLF);$rply = $this->get_lines();$code = substr($rply,0,3);if($this->do_debug >= 2) {echo "S\x4d\124\120 \055\x3e\x20\106\122\x4f\x4d\x20\123E\x52\126E\122\x3a\x20" . $this->CRLF . $rply;}if($code != 250) {$this->error =array("\145\x72\x72\x6f\162" => $hello . " \x6e\x6ft \141\143\x63\x65\160\164\x65\144\x20\x66\x72om\040s\x65r\166e\162","s\x6d\x74\x70_\143ode" => $code,"\163\155t\160_\x6d\163g" => substr($rply,4));if($this->do_debug >= 1) {echo "S\115T\120 \x2d\x3e\040\105\122\x52\117\122\072\040" . $this->error["e\x72ro\162"] . "\x3a\040" . $rply . $this->CRLF;}return false;}$this->helo_rply = $rply;return true;}function Help($keyword="") {$this->error = null; if(!$this->connected()) {$this->error = array("e\162\x72\x6fr" => "Ca\154\154\x65\x64\x20\110\145l\x70\050)\x20\x77\x69t\x68\x6fu\164 \x62e\151ng\040c\157n\156\145c\164e\144");return false;}$extra = "";if(!empty($keyword)) {$extra = " " . $keyword;}fputs($this->smtp_conn,"\x48ELP" . $extra . $this->CRLF);$rply = $this->get_lines();$code = substr($rply,0,3);if($this->do_debug >= 2) {echo "\123\115T\120\040\x2d> \x46RO\x4d SER\x56\x45\122\x3a" . $this->CRLF . $rply;}if($code != 211 && $code != 214) {$this->error =array("\x65\x72\x72\157\162" => "\110\105\x4cP\040n\x6f\164 \x61c\143\145\160\164\145\144\x20\146r\157m\040se\x72\x76e\162","\x73\155\164\160\137\143\157de" => $code,"sm\x74\x70\137\155\163\x67" => substr($rply,4));if($this->do_debug >= 1) {echo "\123\x4dT\120\x20-\076\040ER\122\117R\072 " . $this->error["e\162\x72\157\x72"] . "\x3a\x20" . $rply . $this->CRLF;}return false;}return $rply;}function Mail($from) {$this->error = null; if(!$this->connected()) {$this->error = array("e\x72\162\x6f\x72" => "C\141\154l\145d\x20\115\x61\151\154\050\x29 \x77\x69\x74\x68\x6f\165\164 be\x69\156\147 \x63\157\x6e\x6e\145cte\x64");return false;}$useVerp = ($this->do_verp ? "\x58\x56E\x52P" : "");fputs($this->smtp_conn,"\x4d\x41\111L \106\122O\115\072<" . $from . "\x3e" . $useVerp . $this->CRLF);$rply = $this->get_lines();$code = substr($rply,0,3);if($this->do_debug >= 2) {echo "S\115T\120\x20-\x3e \x46\x52O\115\x20\x53\105R\x56\x45R\x3a" . $this->CRLF . $rply;}if($code != 250) {$this->error =array("er\162\157r" => "\x4d\x41I\114\040\x6e\157\x74\x20\x61\143ce\x70\164\145d\x20fr\157m\040s\145r\x76\145\162","\163m\164\x70\137co\x64\145" => $code,"\163m\164\160\137\x6d\x73\x67" => substr($rply,4));if($this->do_debug >= 1) {echo "\x53\115\124P\x20\055\x3e \105R\x52\117\x52\x3a\x20" . $this->error["err\x6f\162"] . "\x3a " . $rply . $this->CRLF;}return false;}return true;}function Noop() {$this->error = null; if(!$this->connected()) {$this->error = array("\145\162r\157r" => "\103\x61\x6cl\145d\040N\x6fo\160\x28\x29\x20\167it\150\157u\x74\040\x62\145\x69ng\x20\143on\156\145ct\145d");return false;}fputs($this->smtp_conn,"NO\x4f\x50" . $this->CRLF);$rply = $this->get_lines();$code = substr($rply,0,3);if($this->do_debug >= 2) {echo "SM\124\120 \055\076 \106\x52\x4f\115\x20\123\x45\122\126E\122\072" . $this->CRLF . $rply;}if($code != 250) {$this->error =array("\x65r\x72\157\162" => "\116O\x4f\120\x20\156o\x74 \141c\x63\145\x70\164\145d \146\162\x6f\x6d\x20\x73\x65\162\166\x65\x72","smt\x70\x5fc\157\144\x65" => $code,"sm\164\x70_\155\163\x67" => substr($rply,4));if($this->do_debug >= 1) {echo "SM\x54\120\040\055\x3e \x45\x52ROR\072 " . $this->error["\x65r\x72o\162"] . "\072\x20" . $rply . $this->CRLF;}return false;}return true;}function Quit($close_on_error=true) {$this->error = null; if(!$this->connected()) {$this->error = array("e\x72r\157\x72" => "C\x61l\x6c\145\144\x20Q\x75\x69\x74\050)\040\167\151t\x68\x6f\165t\x20\142e\x69\x6eg\x20\143o\x6en\145\x63\164e\x64");return false;} fputs($this->smtp_conn,"\x71\x75i\x74" . $this->CRLF); $byemsg = $this->get_lines();if($this->do_debug >= 2) {echo "S\x4d\x54P\x20\x2d>\x20\106\122\x4f\115 \123\x45\x52V\105R\x3a" . $this->CRLF . $byemsg;}$rval = true;$e = null;$code = substr($byemsg,0,3);if($code != 221) { $e = array("\x65\162ro\x72" => "S\115\x54\120 \163er\166\145\x72 \x72\145\152e\143t\145d\x20\161\165\x69\x74 \143\x6fm\155\x61nd", "\163\155\164\x70_co\x64\145" => $code, "s\155\164p\137\162\160l\x79" => substr($byemsg,4));$rval = false;if($this->do_debug >= 1) {echo "SMT\120\040\055\076\x20\105R\122OR\072\x20" . $e["\x65rro\162"] . "\x3a\040" . $byemsg . $this->CRLF;}}if(empty($e) || $close_on_error) {$this->Close();}return $rval;}function Recipient($to) {$this->error = null; if(!$this->connected()) {$this->error = array("\145r\x72\157\162" => "C\x61l\x6ce\x64\x20R\x65cipi\145\x6e\x74\x28\051 wi\164\x68o\x75\164\x20be\151\x6e\147 \143\157\x6en\x65\x63te\x64");return false;}fputs($this->smtp_conn,"\122C\120T\040\x54O\x3a\x3c" . $to . "\x3e" . $this->CRLF);$rply = $this->get_lines();$code = substr($rply,0,3);if($this->do_debug >= 2) {echo "\x53\x4d\124\x50\040-\076\040\x46R\117M\x20\123\105\122V\105\122\x3a" . $this->CRLF . $rply;}if($code != 250 && $code != 251) {$this->error =array("\x65\162\x72\x6fr" => "\x52\x43\x50\124\040n\x6f\x74\x20\141c\x63\x65\x70t\145d\040f\162om\x20\x73\x65r\x76\x65\162","s\x6d\164\160\x5fc\157d\x65" => $code,"\163\155\x74\x70_\155\163\x67" => substr($rply,4));if($this->do_debug >= 1) {echo "\x53\115\124P \x2d\x3e E\122RO\122: " . $this->error["e\162\x72\x6fr"] . "\072\x20" . $rply . $this->CRLF;}return false;}return true;}function Reset() {$this->error = null; if(!$this->connected()) {$this->error = array("e\x72\x72\157r" => "C\141\x6c\x6ced \x52e\163et\050\x29 wit\150o\165\x74\040be\x69\x6e\x67\x20\x63o\156\x6e\x65c\x74\x65\x64");return false;}fputs($this->smtp_conn,"\122\x53\105T" . $this->CRLF);$rply = $this->get_lines();$code = substr($rply,0,3);if($this->do_debug >= 2) {echo "S\x4d\124\x50 \x2d\x3e \x46R\x4f\x4d\x20\123E\x52\x56\x45R:" . $this->CRLF . $rply;}if($code != 250) {$this->error =array("\145\162r\x6fr" => "RS\105\124\040\146\x61\x69\154e\x64","\163m\x74p_\143\x6f\x64\x65" => $code,"\163\x6d\164\160\137\155\x73\147" => substr($rply,4));if($this->do_debug >= 1) {echo "\x53\x4dT\x50\040\055\x3e\x20\x45\x52R\117R\072 " . $this->error["\x65r\162o\x72"] . "\072\040" . $rply . $this->CRLF;}return false;}return true;}function Send($from) {$this->error = null; if(!$this->connected()) {$this->error = array("\x65r\162\157\162" => "C\x61\x6c\154\x65\144\040S\145n\x64\050\x29 \x77i\x74h\157u\x74\040\142\145\151\156\x67\040c\157\x6e\x6e\x65c\164ed");return false;}fputs($this->smtp_conn,"\123\x45ND\040\x46\x52\x4fM:" . $from . $this->CRLF);$rply = $this->get_lines();$code = substr($rply,0,3);if($this->do_debug >= 2) {echo "\x53MT\x50 \055\076\x20\x46\x52\117\115 \123\105\122V\x45R:" . $this->CRLF . $rply;}if($code != 250) {$this->error =array("e\162\x72o\162" => "\x53E\x4e\x44 \x6e\x6ft\040a\143ce\x70t\145d \146\162o\155\x20s\145\x72\x76e\162","\x73\155\164\160\137\x63\157\x64e" => $code,"s\x6d\x74p\137m\x73\147" => substr($rply,4));if($this->do_debug >= 1) {echo "S\x4d\x54\120 \x2d\x3e\040E\122\122\x4f\122\x3a " . $this->error["er\x72or"] . "\x3a\x20" . $rply . $this->CRLF;}return false;}return true;}function SendAndMail($from) {$this->error = null; if(!$this->connected()) {$this->error = array("\145r\x72o\162" => "\103\x61\x6cled\x20\x53\145\156\144\101\156d\115\141i\154(\051 \167it\x68\157\x75t\040\x62e\x69ng\x20\x63o\x6en\145ct\x65\144");return false;}fputs($this->smtp_conn,"\x53\x41M\x4c\x20\x46R\x4f\115\072" . $from . $this->CRLF);$rply = $this->get_lines();$code = substr($rply,0,3);if($this->do_debug >= 2) {echo "SM\124\120\040-\x3e\040\106\122\x4f\x4d S\x45RV\x45R\x3a" . $this->CRLF . $rply;}if($code != 250) {$this->error =array("\145r\162\157\162" => "\x53A\x4d\114\x20n\157\164\040\x61c\143\145\160t\145d\040\x66rom\040s\x65\x72\166\145\x72","\163\x6d\x74\x70\137\143\x6f\144e" => $code,"\x73m\x74p\137\155\163\147" => substr($rply,4));if($this->do_debug >= 1) {echo "S\115\x54\120 -\x3e \105\122\122OR:\040" . $this->error["\145rr\157r"] . "\x3a\040" . $rply . $this->CRLF;}return false;}return true;}function SendOrMail($from) {$this->error = null; if(!$this->connected()) {$this->error = array("e\x72\162or" => "C\x61\x6c\x6c\145\144 \x53\145ndO\x72\x4d\x61il\x28)\x20\x77\x69t\150\157\x75\x74\x20\142e\x69\156\x67\040con\x6e\145\143\164e\144");return false;}fputs($this->smtp_conn,"\123\x4fM\x4c\040F\122O\115\x3a" . $from . $this->CRLF);$rply = $this->get_lines();$code = substr($rply,0,3);if($this->do_debug >= 2) {echo "\123\115\124P \055\x3e\x20F\122\x4f\115\x20S\x45R\126\105\x52\x3a" . $this->CRLF . $rply;}if($code != 250) {$this->error =array("\x65r\162\x6f\x72" => "S\x4f\115\x4c\x20n\157\x74\040\x61\143c\145p\164e\x64\x20\x66\162\x6f\155\040\x73\x65r\x76\145\162","\163\155\x74\160\137c\157\x64\x65" => $code,"\x73\155\164p\x5fm\x73\x67" => substr($rply,4));if($this->do_debug >= 1) {echo "S\x4dT\120\x20->\x20\x45\122\122O\x52\x3a\040" . $this->error["\x65\x72\162o\x72"] . "\072 " . $rply . $this->CRLF;}return false;}return true;}function Turn() {$this->error = array("e\162\162o\x72" => "\124h\151\x73 \x6d\145\164\x68\x6f\x64\x2c \124URN\x2c\040\x6ff \164\x68e\x20\123\115\x54P\040"."is\040no\164\x20i\155\x70l\x65\x6d\x65\x6e\x74\145\144");if($this->do_debug >= 1) {echo "\123\x4dT\x50\040\x2d>\040\x4e\x4fT\x49\103E\072\x20" . $this->error["e\x72\x72\157\x72"] . $this->CRLF;}return false;}function Verify($name) {$this->error = null; if(!$this->connected()) {$this->error = array("\145\x72ror" => "C\x61\154\154\145\x64\040\x56er\151\x66y(\x29 w\151\x74h\157u\164\040\142\145\x69\156\147\x20c\x6f\x6e\156\145\x63te\144");return false;}fputs($this->smtp_conn,"\x56R\x46\x59\x20" . $name . $this->CRLF);$rply = $this->get_lines();$code = substr($rply,0,3);if($this->do_debug >= 2) {echo "S\115\124\120\x20\055>\040\106\x52\117M\x20\x53\105\x52\x56\x45R:" . $this->CRLF . $rply;}if($code != 250 && $code != 251) {$this->error =array("\x65r\162\x6fr" => "\126RF\131 \x66a\151\x6c\x65\144 \157\156\040\156\x61\x6d\145 '$name'","\163\x6d\x74\x70\137\x63od\x65" => $code,"s\155\164p\x5f\155\x73\147" => substr($rply,4));if($this->do_debug >= 1) {echo "\123\x4d\x54\x50 \055\x3e\x20ER\122\x4fR\072\040" . $this->error["\x65r\x72\157\162"] . ":\x20" . $rply . $this->CRLF;}return false;}return $rply;}function get_lines() {$data = "";while($str = @fgets($this->smtp_conn,515)) {if($this->do_debug >= 4) {echo "S\115\124P ->\x20\x67et_li\156\145s\050)\072 \$\144\141t\x61 \x77\141\163\x20\"$data\"" . $this->CRLF;echo "\123\115\124P\x20\x2d> \147\145\x74_l\x69\156\x65\163\050\x29\072\x20\$\x73\x74\x72\x20\151\x73\x20\"$str\"" . $this->CRLF;}$data .= $str;if($this->do_debug >= 4) {echo "\x53\x4dTP\x20-\076 \x67\x65\164_\x6cine\x73\x28\x29:\040\$dat\x61\040\x69\163\x20\"$data\"" . $this->CRLF;} if(substr($str,3,1) == " ") { break; }}return $data;}}

// Errors and warnings
//error_reporting(E_ALL);
error_reporting(0);

// Required params
if (count($argv) < 3) {
  die("Usage: ${argv[0]} <op_name> <errcode> [attachment1 [attachment2...]]\n");
}

date_default_timezone_set("Europe/Rome"); // does not work in PHP4

// Error resistance!
$max_attempts = 3;
for ($i=1; $i<=$max_attempts; $i++) {
  $ret = SendMail();
  if ($ret === true) {
    //echo "Mail sent.\n";
    break;
  }
  //else {
  //  echo "Error (attempt #$i): " . $ret . "\n";
  //}
}

// Say last error
if (($ret !== true) && ($i == $max_attempts+1)) {
  echo "Could not send email after $max_attempts attempts, sorry.\n";
  echo "Last error was: " . $ret . "\n";
}

// Actually sends the email.
function SendMail() {
  global $argv;

  $mail = new PHPMailer();
  //$mail->SMTPDebug = true;
  $mail->IsSMTP();
  $mail->Timeout = 60;  // in seconds
  //$mail->CharSet = "utf-8";

  // Here, we include the configuration file. It is searched in the home
  // directory: $HOME/.lfscripts and if not found in the script's directory
  // plus "conf/".
  $confFile = getenv("HOME") . "/.lfscripts/emailme.conf";
  if (file_exists($confFile)) {
    require $confFile;
  }
  else {
    $confFile = dirname(__FILE__) . "/conf/emailme.conf";
    if (file_exists($confFile)) {
      require $confFile;
    }
    else {
      die("Can't find configuration file!");
    }
  }
  
  $mail->FromName = GetShortHostName();


  $mail->Subject = $argv[1] . ": " . (($argv[2] == 0) ? "success!" : "errors");

  for ($i=3; $i<count($argv); $i++) {
    if ($mail->AddAttachment($argv[$i]) === false) {
      die("Problems attaching file \"" . $argv[$i] . "\"\n");
    }
  }

  $mail->Body = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\">" .
                "<tr><td><b>Operation:</b></td><td>${argv[1]}</td></tr>" .
                "<tr><td><b>Errorcode:</b></td><td>${argv[2]}</td></tr>" .
                "<tr><td><b>Completed at:</b></td><td>" .
                date("D M d H:i:s T Y") . "</td></tr></table>";

  $mail->WordWrap = 60; // set word wrap
  $mail->IsHTML(true); // send as HTML

  if (!$mail->Send()) {
    return $mail->ErrorInfo;
  }

  return true;
}

function GetShortHostName() {
  $handler = popen("hostname -s", "r");
  $hostname = stream_get_line($handler, 1000);
  pclose($handler);
  return $hostname;
}

?>
